/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SeriesPropertiesDialog.java
 *
 * Created on 2012-02-26, 20:39:57
 */
package JFreeChartExtensions.ChartPanelDialogs;


import JFreeChartExtensions.chartextensions.inset.InsetExtension;
import JFreeChartExtensions.Collections.DatasetCollection;
import JFreeChartExtensions.ExtendableChartPanel;
import JFreeChartExtensions.ExtendedXYPlot;
import JFreeChartExtensions.renderers.XYErrorRendererExtended;
import java.awt.Color;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYIntervalSeries;

/**
 *
 * @author Nobody
 */
public class SeriesDetailsDialog extends JDialog {

    private ExtendableChartPanel panel;
    private int seriesIndex;
    private XYLineAndShapeRenderer errorRenderer;
    private XYIntervalSeries series;
    private int dataset;

    public SeriesDetailsDialog(String seriesName, int dataset, ExtendableChartPanel panel, JDialog parent) {
        super(parent);
        setAlwaysOnTop(true);
        setLocationRelativeTo(parent);
        initComponents();

        DatasetCollection collection = panel.getDatasetCollection();
       // XYReggresionSeriesCollection collection = panel.getDataSeriesCollections().get(dataset);

        this.panel = panel;
        this.seriesIndex = 0;// collection.getSeriesIndex(seriesName);
        this.dataset = dataset;
        this.series = collection.getSeriesByName(seriesName);
        SeriesName.setText(seriesName);
        NameEdit.setText(seriesName);
        errorRenderer = panel.getDatasetCollection().getMasterRenderer().getRenderersCollections().get(dataset);
        extractSeriesPropertiesToUIControls();
    }

    private void extractSeriesPropertiesToUIControls() {
        if (errorRenderer.getSeriesShapesVisible(seriesIndex) == null) {
            SeriesShapeCheckBox.setSelected(true);
        } else {
            SeriesShapeCheckBox.setSelected(errorRenderer.getSeriesShapesVisible(seriesIndex));
        }
        if (errorRenderer.getSeriesLinesVisible(seriesIndex) == null) {
            LineVisibleCheckBox.setSelected(false);
        } else {
            LineVisibleCheckBox.setSelected(errorRenderer.getSeriesLinesVisible(seriesIndex));
        }
        if (errorRenderer instanceof XYErrorRendererExtended) {
            ErrorBarsVisibleCheckBox.setSelected(((XYErrorRendererExtended) errorRenderer).getSeriesYError(seriesIndex));
        }

    }

    private void saveSettings() {
        errorRenderer.setSeriesShapesVisible(seriesIndex, SeriesShapeCheckBox.isSelected());
        errorRenderer.setSeriesLinesVisible(seriesIndex, LineVisibleCheckBox.isSelected());
        if (errorRenderer instanceof XYErrorRendererExtended) {
            ((XYErrorRendererExtended) errorRenderer).setSeriesYError(seriesIndex, ErrorBarsVisibleCheckBox.isSelected());
            ((XYErrorRendererExtended) errorRenderer).setSeriesXError(seriesIndex, false);
        }
        panel.updateChart();
        if (!((String) series.getKey()).equals(NameEdit.getText())) {
            series.setKey(NameEdit.getText());
        }
        panel.getDatasetCollection().applyGlobalRendererSettings(dataset, seriesIndex, (XYErrorRendererExtended) errorRenderer);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        colorChooser = new javax.swing.JColorChooser();
        mainPanel = new javax.swing.JPanel();
        propertiesPanel = new javax.swing.JPanel();
        colorLabel = new javax.swing.JLabel();
        setColorButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        NameEdit = new javax.swing.JTextField();
        SeriesShapeCheckBox = new javax.swing.JCheckBox();
        LineVisibleCheckBox = new javax.swing.JCheckBox();
        ErrorBarsVisibleCheckBox = new javax.swing.JCheckBox();
        SeriesPanel = new javax.swing.JPanel();
        SeriesName = new javax.swing.JLabel();
        firstAxisMapper = new javax.swing.JButton();
        SecondAxisMapper = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        ButtonsPanel = new javax.swing.JPanel();
        OkButton = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        propertiesPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Properties"));

        colorLabel.setText("Color :");

        setColorButton.setText("Set Color...");
        setColorButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                setColorButtonMouseClicked(evt);
            }
        });

        jLabel1.setText("Name");

        SeriesShapeCheckBox.setText("Shapes visible");

        LineVisibleCheckBox.setText("Line visible");

        ErrorBarsVisibleCheckBox.setText("Error bars visible");

        javax.swing.GroupLayout propertiesPanelLayout = new javax.swing.GroupLayout(propertiesPanel);
        propertiesPanel.setLayout(propertiesPanelLayout);
        propertiesPanelLayout.setHorizontalGroup(
            propertiesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(propertiesPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(propertiesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(ErrorBarsVisibleCheckBox)
                    .addComponent(LineVisibleCheckBox)
                    .addComponent(SeriesShapeCheckBox)
                    .addGroup(propertiesPanelLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(40, 40, 40)
                        .addComponent(NameEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(propertiesPanelLayout.createSequentialGroup()
                        .addComponent(colorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 102, Short.MAX_VALUE)
                        .addComponent(setColorButton, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        propertiesPanelLayout.setVerticalGroup(
            propertiesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(propertiesPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(propertiesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(NameEdit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(SeriesShapeCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(LineVisibleCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(ErrorBarsVisibleCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(propertiesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(setColorButton)
                    .addComponent(colorLabel))
                .addContainerGap())
        );

        SeriesName.setText("Series Name");
        SeriesPanel.add(SeriesName);

        firstAxisMapper.setText("Map to first axis range");
        firstAxisMapper.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                firstAxisMapperMouseClicked(evt);
            }
        });

        SecondAxisMapper.setText("Map to second axis range");
        SecondAxisMapper.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                SecondAxisMapperMouseClicked(evt);
            }
        });

        jButton1.setText("Bring to top");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(SeriesPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 326, Short.MAX_VALUE)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(propertiesPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(10, 10, 10))
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(firstAxisMapper, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(SecondAxisMapper, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(161, Short.MAX_VALUE))
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
                .addGap(161, 161, 161))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addComponent(SeriesPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(propertiesPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(firstAxisMapper)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(SecondAxisMapper)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap(17, Short.MAX_VALUE))
        );

        OkButton.setText("OK");
        OkButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                OkButtonMouseClicked(evt);
            }
        });
        ButtonsPanel.add(OkButton);

        CancelButton.setText("Cancel");
        CancelButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                CancelButtonMouseClicked(evt);
            }
        });
        ButtonsPanel.add(CancelButton);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(ButtonsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 306, Short.MAX_VALUE)
                .addContainerGap())
            .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ButtonsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void setColorButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_setColorButtonMouseClicked
        Color c = (Color) errorRenderer.getSeriesPaint(seriesIndex);
        if ((c = JColorChooser.showDialog(panel, "Choose Color for Series", c)) != null) {
            errorRenderer.setSeriesPaint(seriesIndex, c);
        }
    }//GEN-LAST:event_setColorButtonMouseClicked

    private void CancelButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_CancelButtonMouseClicked
        this.dispose();
    }//GEN-LAST:event_CancelButtonMouseClicked

    private void OkButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_OkButtonMouseClicked
        saveSettings();
        this.dispose();
    }//GEN-LAST:event_OkButtonMouseClicked

    private void firstAxisMapperMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_firstAxisMapperMouseClicked
        panel.mapDatasetToRangeAxisAndNotifyListeners(seriesIndex, 0);
        panel.updateChart();
    }//GEN-LAST:event_firstAxisMapperMouseClicked

    private void SecondAxisMapperMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_SecondAxisMapperMouseClicked
        panel.mapDatasetToRangeAxisAndNotifyListeners(seriesIndex, 1);
        panel.updateChart();
    }//GEN-LAST:event_SecondAxisMapperMouseClicked

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        ExtendedXYPlot plot = (ExtendedXYPlot) panel.getChart().getXYPlot();
        InsetExtension insetExtension = ((InsetExtension) panel.getExtensionByName("InsetExtension"));
        if (insetExtension != null) {
            ExtendedXYPlot extendedXYPlot = ((ExtendedXYPlot) insetExtension.getInset().getChartPanel().getChart().getXYPlot());
            extendedXYPlot.setSeriesToTop(dataset);
            insetExtension.getInset().getChartPanel().repaint();
        }
        plot.setSeriesToTop(dataset);
    }//GEN-LAST:event_jButton1MouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel ButtonsPanel;
    private javax.swing.JButton CancelButton;
    private javax.swing.JCheckBox ErrorBarsVisibleCheckBox;
    private javax.swing.JCheckBox LineVisibleCheckBox;
    private javax.swing.JTextField NameEdit;
    private javax.swing.JButton OkButton;
    private javax.swing.JButton SecondAxisMapper;
    private javax.swing.JLabel SeriesName;
    private javax.swing.JPanel SeriesPanel;
    private javax.swing.JCheckBox SeriesShapeCheckBox;
    private javax.swing.JColorChooser colorChooser;
    private javax.swing.JLabel colorLabel;
    private javax.swing.JButton firstAxisMapper;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JPanel propertiesPanel;
    private javax.swing.JButton setColorButton;
    // End of variables declaration//GEN-END:variables
}
